package com.jf3q.sup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf3q.sup.domain.Employees;
import com.jf3q.sup.domain.Roles;
import com.jf3q.sup.domain.Users;
import com.jf3q.sup.mapper.EmployeesMapper;
import com.jf3q.sup.service.UsersService;
import com.jf3q.sup.mapper.UsersMapper;
import com.jf3q.sup.vo.PersonalVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users>
        implements UsersService {


    @Autowired
    UsersMapper usersMapper;
    @Autowired
    EmployeesMapper employeesMapper;

    /**
     * 获取security框架认识的用户
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Users user = usersMapper.selectOne(new LambdaQueryWrapper<Users>().eq(Users::getUsername, username));
        if (user == null) {
            throw new UsernameNotFoundException("账号不存在!!!\n请检查账号后再重新登录~");
        }
        // 根据用户id查询当前用户所拥有的权限及资源
        List<Roles> roles = usersMapper.selectRoleWithResource(user.getId());
        user.setRoles(roles);
        return user;
    }

    @Override
    public PersonalVo selectPersonalVo() {
        // 从SecurityContextHolder上下文中获取出当时登录存放的用户账号
        String username = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        Users user = (Users) loadUserByUsername(username);
        return usersMapper.selectPerSonal(user.getId());
    }

    @Transactional
    @Override
    public void updatePersonal(PersonalVo personalVo, PasswordEncoder passwordEncoder) {
        Users users = usersMapper.selectOne(new LambdaQueryWrapper<Users>().eq(Users::getId, personalVo.getId()));
        // 根据数据库里的密码跟前端传过来的密码做对比  相同无需修改  若不相同则需要重新生成密码
        if (!users.getPassword().equals(personalVo.getPassword())) {
            // 使用passwordEncoder进行加盐加密
            users.setPassword(passwordEncoder.encode(personalVo.getPassword()));
        }
        users.setUsername(personalVo.getUsername());
        users.setUpdatedAt(new Date());
        // 根据用户id修改用户的基本信息
        usersMapper.updateById(users);

        Employees employees = new Employees();
        employees.setUserId(users.getId());
        employees.setName(personalVo.getName());
        employees.setEmail(personalVo.getEmail());
        employees.setAddress(personalVo.getAddress());
        employees.setPhone(personalVo.getPhone());
        employees.setUpdatedAt(new Date());
        // 根据用户id去修改员工的基本信息
        employeesMapper.update(employees, new LambdaQueryWrapper<Employees>().eq(Employees::getUserId, users.getId()));
    }
}




